package com.hoshiicloud.auth.service;

import com.hoshiicloud.UsersLoginVO;
import com.hoshiicloud.auth.feign.UserFeignService;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.constant.UserTypeEnum;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.BackSysUsersVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class UserService {

    @Autowired
    private UserFeignService userFeignService;

    public BackSysUsersVO getUser(String username, String userType) {
        Response<UsersLoginVO> result;
        if (UserTypeEnum.SAAS.getUserType().equals(userType)) {
            //获取saas用户信息
            result = userFeignService.getSysUser(username);

        } else if (UserTypeEnum.COMPANY.getUserType().equals(userType)) {
            //获取租户登录信息
            result = userFeignService.getComUser(username);
        } else {
            throw new ServiceException("未匹配到用户类型");
        }
        if (null == result) {
            return null;
        }
        log.info("返回结果:" + result.toString());

        if (result.isSuccess()) {
            BackSysUsersVO user = new BackSysUsersVO();
            UsersLoginVO usersLoginVO = result.getData();
            user.setUserName(usersLoginVO.getUsername());
            user.setRealName(usersLoginVO.getRealName());
            user.setPassword(usersLoginVO.getPassword());
            user.setUserType(userType);
            user.setLocked(usersLoginVO.getLocked());
            user.setRole(StringUtils.join(usersLoginVO.getRoleList(), ","));
            user.setPermissionUrl(StringUtils.join(usersLoginVO.getPermissionUrlList(), ","));
            user.setPermission(StringUtils.join(usersLoginVO.getPermissionValueList(), ","));
            return user;
        } else {
            throw new ServiceException(result.getMsg());
        }
    }

    public TokenUsersVO getStoreUser(String username, Long storeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("storeId", storeId);
        Response<TokenUsersVO> result = userFeignService.getStoreMemberUser(map);
        if (null == result || result.getCode() != 200) {
            throw new ServiceException(result.getMsg());
        }
        return result.getData();

    }

    public TokenUsersVO getCompanyUser(String username, String privateDomain) {
        Map<String, String> map = new HashMap<>(16);
        map.put("username", username);
        map.put("privateDomain", privateDomain);
        Response<TokenUsersVO> result = userFeignService.getCompanyUser(map);
        if (null == result || result.getCode().equals(200)) {
            throw new ServiceException(result.getMsg());
        }
        return result.getData();
    }
}
